﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TeamBuy.AccessWebUI.Account
{
    public partial class Setting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (TeamBuySession.IsUserLoggedIn == true)
                {
                    TeamBuy.AccessService.TeamBuyUserService getUser = new AccessService.TeamBuyUserService();
                    DataTable dtUser = getUser.GetUser(Convert.ToInt32(Session["UserID"]));
                    txtEmail.Text = dtUser.Rows[0]["UserEmail"].ToString();
                    txtUserName.Text = dtUser.Rows[0]["UserName"].ToString();
                    txtMobile.Text = dtUser.Rows[0]["MobileNumber"].ToString();
                }
            }
        }

        protected void btnSubmitSetting_Click(object sender, EventArgs e)
        {
            bool isUpdateUserName=false;
            bool isUpdatePwd=false;
            TeamBuy.AccessService.TeamBuyUserService updateUser = new AccessService.TeamBuyUserService();
            if (txtUserName.Text != "")
            {
                if (txtUserName.Text.Length >= 2 && txtUserName.Text.Length <= 8)
                {
                    isUpdateUserName = true;
                    if (isUpdateUserName == true && txtPassword.Text != "")
                    {
                        if (txtPassword.Text.Length >= 4)
                        {
                            if (txtPassword.Text == txtRePassword.Text)
                            {
                                isUpdatePwd = true;
                            }
                            else
                            {
                                Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                                Main_PublicSystemMessage1.MessageVisible = true;
                                Main_PublicSystemMessage1.Message = "两次输入密码必须相同";
                                //repwd not same
                            }
                        }
                        else
                        {
                            Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                            Main_PublicSystemMessage1.MessageVisible = true;
                            Main_PublicSystemMessage1.Message = "密码长度必须大于 4 个字符";
                            //length not >4
                        }
                    }


                }
                else
                {
                    Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                    Main_PublicSystemMessage1.MessageVisible = true;
                    Main_PublicSystemMessage1.Message = "用户名必须 2 到 8 个字符";
                    //2-8
                }
            }
            else
            {
                Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                Main_PublicSystemMessage1.MessageVisible = true;
                Main_PublicSystemMessage1.Message = "用户名不能为空";

            }
            if (isUpdatePwd == false && isUpdateUserName == true)
            {
                updateUser.UpdateUserSetting(Convert.ToInt32(Session["UserID"]), txtUserName.Text, txtMobile.Text);
                TeamBuySession.CreateUserSession(txtEmail.Text, Session["UserPwd"].ToString(), false);
                Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Success;
                Main_PublicSystemMessage1.MessageVisible = true;
                Main_PublicSystemMessage1.Message = "操作成功";
            }
            else if (isUpdatePwd == true && isUpdateUserName == true)
            {
                updateUser.ChangePassword(Convert.ToInt32(Session["UserID"]), Session["UserPwd"].ToString(), txtPassword.Text);
                updateUser.UpdateUserSetting(Convert.ToInt32(Session["UserID"]), txtUserName.Text, txtMobile.Text);

                TeamBuySession.CreateUserSession(txtEmail.Text, txtPassword.Text, false);
                //success
                Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Success;
                Main_PublicSystemMessage1.MessageVisible = true;
                Main_PublicSystemMessage1.Message = "操作成功";

            }
        }
    }
}